package com.example.weather;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 古嘉华 on 2018/4/26.
 */

public class TempratureView extends View {
    private Paint paint;
    private Temprature[] tempratures;
    private Point[] highPoints = new Point[5];
    private Point[] lowPoints= new Point[5];
    private List<WeatherData.DataBean.ForecastBean> forecast;


    public TempratureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        Log.e("TempratureViewOnDraw", "forecast");
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextSize(20);
        paint.setStyle(Paint.Style.STROKE);

    }
    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.drawColor(Color.CYAN);
        super.onDraw(canvas);
        Path highPath = new Path();
       try {
           for (int i = 0; i < highPoints.length; i++) {
//               Log.e("TempratureView", "X:" + highPoints[i].x + "Y:" + highPoints[i].y);
               if (i == 0) {
                   highPath.moveTo(highPoints[i].x, highPoints[i].y);
               } else {
                   highPath.lineTo(highPoints[i].x, highPoints[i].y);
               }
               paint.setStrokeWidth(10);
               paint.setColor(Color.rgb(0,0,130));
               paint.setAntiAlias(true);
               canvas.drawPoint(highPoints[i].x, highPoints[i].y, paint);
//               Log.e("TempratureView","msg:"+tempratures[i].high);
               paint.setColor(Color.BLACK);
               paint.setStrokeWidth(1);
               canvas.drawText(String.valueOf(tempratures[i].high)+"℃",highPoints[i].x-10,highPoints[i].y-15,paint);

           }
           paint.setColor(Color.rgb(0,0,130));
           paint.setStrokeWidth(2);
           canvas.drawPath(highPath, paint);

           Path lowPath = new Path();
           for (int i = 0; i < lowPoints.length; i++) {
               if (i == 0) {
                   lowPath.moveTo(lowPoints[i].x, lowPoints[i].y);
               } else {
                   lowPath.lineTo(lowPoints[i].x, lowPoints[i].y);
               }
               paint.setStrokeWidth(10);
               paint.setColor(Color.YELLOW);
               canvas.drawPoint(lowPoints[i].x, lowPoints[i].y, paint);
               paint.setStrokeWidth(1);
               paint.setColor(Color.BLACK);
               canvas.drawText(String.valueOf(tempratures[i].low)+"℃",lowPoints[i].x-10,lowPoints[i].y+30,paint);
           }
           paint.setColor(Color.YELLOW);
           paint.setStrokeWidth(2);
           canvas.drawPath(lowPath, paint);
       }catch (Exception e){
           e.printStackTrace();
       }
       invalidate();
    }


    public void setData(List<WeatherData.DataBean.ForecastBean> forecast) {
//        Log.e("TempratureView","msg:"+forecast.get(0).getHigh().substring(3,5));
        tempratures = new Temprature[5];
        tempratures[0] = new Temprature(Integer.parseInt(forecast.get(0).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(0).getLow().substring(3,5)));
        tempratures[1] = new Temprature(Integer.parseInt(forecast.get(1).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(1).getLow().substring(3,5)));
        tempratures[2] = new Temprature(Integer.parseInt(forecast.get(2).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(2).getLow().substring(3,5)));
        tempratures[3] = new Temprature(Integer.parseInt(forecast.get(3).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(3).getLow().substring(3,5)));
        tempratures[4] = new Temprature(Integer.parseInt(forecast.get(4).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(4).getLow().substring(3,5)));

        for(int i=0; i<tempratures.length;i++){
            highPoints[i] = new Point(130*(i+1), (50 - tempratures[i].high) * 5);
            lowPoints[i] = new Point(130*(i+1), (50 - tempratures[i].low) * 5);
//            Log.e("TempratureView","msg:"+tempratures[i].high);
//            Log.e("TempratureView","highPoints:"+highPoints[i]+"lowPoints:"+lowPoints[i]);
        }

//        Log.e("TempratureView","highPointsX:"+highPoints[0].x+"   highPointsY:"+highPoints[0].y);
    }


}
